package com.ossjk.oa.advisory.service.impl;

import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.advisory.entity.Counterpartfw;
import com.ossjk.oa.advisory.entity.School;
import com.ossjk.oa.advisory.mapper.SchoolMapper;
import com.ossjk.oa.advisory.service.ICounterpartfwService;
import com.ossjk.oa.advisory.service.ISchoolService;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 学校 服务实现类
 * </p>
 *
 * @author Jason
 * @since 2019-02-04
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements ISchoolService {
	@Autowired
	private ICounterpartfwService iCounterpartService;
	@Autowired
	private ISchoolService iSchoolService;

	@Override
	public Page selectVoPage(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoPage(page, wrapper));
		return page;
	}

	@Override
	public boolean updateDetail(School school, String[] cname, String[] cphone, String[] cqq, String[] cwechat, String[] cername, String[] ceid, String[] cremarks) {
		// TODO Auto-generated method stub
		// 新增联系人
		if (!CommonUtil.isBlank(cname) && !CommonUtil.isBlank(cphone) && !CommonUtil.isBlank(cqq) && !CommonUtil.isBlank(cwechat) && !CommonUtil.isBlank(cername) && !CommonUtil.isBlank(ceid)) {
			if (cname.length == cphone.length && cname.length == cqq.length && cname.length == cwechat.length && cname.length == cername.length && cname.length == ceid.length) {
				Wrapper wrapper = Condition.create().like("sid", school.getId());
				iCounterpartService.delete(wrapper);
				List list = new ArrayList();
				for (int i = 0; i < cname.length; i++) {
//					Counterpartfw counterpart = new Counterpartfw();
//					counterpart.setName(cname[i]);
//					counterpart.setSid(school.getId());
//					counterpart.setPhone(new BigDecimal(cphone[i]));
//					counterpart.setQq(new BigDecimal(cqq[i]));
//					counterpart.setWechat(cwechat[i]);
//					counterpart.setEid(ceid[i]);
//					counterpart.setRemarks(cremarks[i]);
//					list.add(counterpart);
				}
				iCounterpartService.insertBatch(list);
			}
		}
		// 更新基础信息
		return this.updateById(school);
	}

	@Override
	public boolean insert(School school, String[] cname, String[] cphone, String[] cqq, String[] cwechat, String[] ceid, String[] cremarks) {
		// TODO Auto-generated method stub
		if (!CommonUtil.isBlank(cname) && !CommonUtil.isBlank(cphone) && !CommonUtil.isBlank(cqq) && !CommonUtil.isBlank(cwechat) && !CommonUtil.isBlank(ceid)) {
			iSchoolService.insert(school);
			if (cname.length == cphone.length && cname.length == cqq.length && cname.length == cwechat.length && cname.length == ceid.length) {
				List list = new ArrayList();
				for (int i = 0; i < cname.length; i++) {
//					Counterpartfw counterpart = new Counterpartfw();
//					counterpart.setName(cname[i]);
//					counterpart.setSid(school.getId());
//					counterpart.setPhone(new BigDecimal(cphone[i]));
//					counterpart.setQq(new BigDecimal(cqq[i]));
//					counterpart.setWechat(cwechat[i]);
//					counterpart.setEid(ceid[i]);
//					counterpart.setRemarks(cremarks[i]);
//
//					list.add(counterpart);
				}
				iCounterpartService.insertBatch(list);
			}
		}
		return true;
	}
}
